package com.nanhai.buddhist.college.module.collegebase.controller.admin.classroominfo;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;
import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.classroominfo.vo.*;
import com.nanhai.buddhist.college.module.collegebase.convert.classroominfo.ClassroomInfoConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classroominfo.ClassroomInfoDO;
import com.nanhai.buddhist.college.module.collegebase.service.classroominfo.ClassroomInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 教室")
@RestController
@RequestMapping("/collegebase/classroom-info")
@Validated
public class ClassroomInfoController {

    @Resource
    private ClassroomInfoService classroomInfoService;

    @PostMapping("/create")
    @ApiOperation("创建教室")
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-info:create')")
    public CommonResult<String> createClassroomInfo(@Valid @RequestBody ClassroomInfoCreateReqVO createReqVO) {
        return success(classroomInfoService.createClassroomInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新教室")
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-info:update')")
    public CommonResult<Boolean> updateClassroomInfo(@Valid @RequestBody ClassroomInfoUpdateReqVO updateReqVO) {
        classroomInfoService.updateClassroomInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除教室")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-info:delete')")
    public CommonResult<Boolean> deleteClassroomInfo(@RequestParam("id") String id) {
        classroomInfoService.deleteClassroomInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得教室")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-info:query')")
    public CommonResult<ClassroomInfoRespVO> getClassroomInfo(@RequestParam("id") String id) {
        ClassroomInfoDO classroomInfo = classroomInfoService.getClassroomInfo(id);

        ClassroomInfoRespVO convert = ClassroomInfoConvert.INSTANCE.convert(classroomInfo);
        return success(convert);
    }

    @GetMapping("/list")
    @ApiOperation("获得教室列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-info:query')")
    public CommonResult<List<ClassroomInfoRespVO>> getClassroomInfoList(@RequestParam("ids") Collection<String> ids) {
        List<ClassroomInfoDO> list = classroomInfoService.getClassroomInfoList(ids);
        return success(ClassroomInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得教室分页")
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-info:query')")
    public CommonResult<PageResult<ClassroomInfoRespVO>> getClassroomInfoPage(@Valid ClassroomInfoPageReqVO pageVO) {
        PageResult<ClassroomInfoDO> pageResult = classroomInfoService.getClassroomInfoPage(pageVO);
        return success(ClassroomInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出教室 Excel")
    @PreAuthorize("@ss.hasPermission('collegebase:classroom-info:export')")
    @OperateLog(type = EXPORT)
    public void exportClassroomInfoExcel(@Valid ClassroomInfoPageReqVO exportReqVO,
              HttpServletResponse response) throws IOException {

        exportReqVO.setPageNo(1);
        exportReqVO.setPageSize(999999);
        PageResult<ClassroomInfoDO> pageResult = classroomInfoService.getClassroomInfoPage(exportReqVO);
       if(pageResult==null) pageResult = new PageResult<ClassroomInfoDO>();
        // 导出 Excel
        List<ClassroomInfoExcelVO> list = ClassroomInfoConvert.INSTANCE.convertList02(pageResult.getList());
        ExcelUtils.write(response, "教室.xls", "数据", ClassroomInfoExcelVO.class, list);
    }

}
